4. 密度プロット

4.1. 概要

密度プロットとは

4.2. Plotlyによる作図方法

Plotlyでは,plotly.figure_factory.create_distplot()でDensity plotを作成可能です.

import plotly.figure_factory as ff
fig = ff.create_distplot(
    df['x_col'].values.reshape(1, -1), 
    ['label'], show_hist=False)

上記の例では,dfcol_x列をX軸,その確率密度をY軸にとったDensity plotのオブジェクトfigを作成します. ただし,labelのように凡例名を指定する必要があることにご注意ください.

show_hist=False

plotly.figure_factory.create_distplot()はデフォルト設定でヒストグラムとDensity plotの両方を作図します.Density plotのみ表示したい場合は,show_hist=Falseを指定しましょう.

4.3. MADB Labを用いた作図例

4.3.1. 下準備

import pandas as pd
import plotly.figure_factory as ff

import warnings
warnings.filterwarnings('ignore')
# 前処理の結果,以下に分析対象ファイルが格納されていることを想定
PATH_DATA = '../../data/preprocess/out/episodes.csv'
# Jupyter Book用のPlotlyのrenderer
RENDERER = 'plotly_mimetype+notebook'
def show_fig(fig):
    """Jupyter Bookでも表示可能なようRendererを指定"""
    fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
    fig.show(renderer=RENDERER)
df = pd.read_csv(PATH_DATA)

4.3.2. 作品別の合計連載週数

df_plot = df.value_counts('cname').reset_index(name='weeks')
fig = ff.create_distplot(
    df_plot['weeks'].values.reshape(1, -1), 
    ['全雑誌'], show_hist=False)
fig.update_layout(title_text='作品別の合計連載週数')
show_fig(fig)
fig.update_xaxes(range=[0, 200])
show_fig(fig)

4.3.3. 雑誌別・作品別の合計連載週数

df_plot = \
    df.value_counts(['mcname', 'cname']).reset_index(name='weeks')
df_plot = \
    df.value_counts(['mcname', 'cname']).reset_index(name='weeks')
# distplot用に中間集計
mcnames = df_plot.mcname.unique()
hist_data = [
    df_plot[df_plot['mcname']==mc]['weeks'].values.reshape(-1)
    for mc in mcnames]
fig = ff.create_distplot(
    hist_data, mcnames, show_hist=False)
fig.update_xaxes(range=[0, 200])
fig.update_layout(title_text='雑誌別・作品別の合計連載週数')
show_fig(fig)

4.3.4. 作品別の合計連載週数

df_plot = df.value_counts('creator').reset_index(name='weeks')
fig = ff.create_distplot(
    df_plot['weeks'].values.reshape(1, -1), 
    ['全雑誌'], show_hist=False)
fig.update_layout(title_text='作者別の合計連載週数')
fig.update_xaxes(range=[0, 200])
show_fig(fig)

4.3.5. 雑誌別・作者別の合計連載週数

df_plot = \
    df.value_counts(['mcname', 'creator']).reset_index(name='weeks')
# distplot用に中間集計
mcnames = df_plot.mcname.unique()
hist_data = [
    df_plot[df_plot['mcname']==mc]['weeks'].values.reshape(-1)
    for mc in mcnames]
fig = ff.create_distplot(
    hist_data, mcnames, show_hist=False)
fig.update_layout(title_text='雑誌別・作者別の合計連載週数')
fig.update_xaxes(range=[0, 200])
show_fig(fig)